﻿using B.S.AuthCenter.Api.Write.Application.Command.Roles;
using B.S.AuthCenter.Domain;
using B.S.AuthCenter.ErrorCount;
using B.S.AuthCenter.InfrasIntructure.Interface;
using MediatR;

namespace B.S.AuthCenter.Api.Write.Application.Handler.Roles
{
    public class DeleteUserRoleHandler : IRequestHandler<DeleteUserRoleCommand, ApiResult<int>>
    {
        IBaseRepository<UserRoleModel> userRoleRepo;

        public DeleteUserRoleHandler(IBaseRepository<UserRoleModel> userRoleRepo)
        {
            this.userRoleRepo = userRoleRepo;
        }

        public Task<ApiResult<int>> Handle(DeleteUserRoleCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res=new ApiResult<int>();
            var ur=userRoleRepo.GetAll().Where(c=>!c.IsDeleted && c.Uid==request.Uid).FirstOrDefault();
            if (ur==null)
            {
                res.Msg = "找不到编号";
                res.Code=ApiEnum.Fail;
                return Task.FromResult(res);
            }
            ur.IsDeleted=true;
            int a=userRoleRepo.Update(ur).Result;
            if(a>0)
            {
                res.Datas = a;
                res.Code = ApiEnum.Success;
                res.Msg = "已删除用户与角色之间的关系";
                return Task.FromResult(res);
            }
            else
            {
                res.Datas = a;
                res.Code = ApiEnum.Fail;
                res.Msg = "未删除用户与角色之间的关系";
                return Task.FromResult(res);
            }
        }
    }
}
